﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;

namespace XYX.Print.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public class Computer
    {

        private const string QueryString = "Select SerialNumber From Win32_BaseBoard";
        private const string CPUQueryString = "Select ProcessorId From Win32_processor";

        public static IdentificationClass GetComputerInfo()
        {
            // Name
            IdentificationClass identification = new IdentificationClass();
            identification.ComputerName = Environment.MachineName;

            // 硬盘
            ManagementObject obp = new ManagementObject(@"Win32_LogicalDisk.deviceid=""c:""");
            obp.Get();
            identification.HardDisk = obp["VolumeSerialNumber"].ToString();

            // 主板
            ManagementObjectSearcher management = new ManagementObjectSearcher(QueryString);
            ManagementObjectCollection baseObjects = management.Get();
            string id = string.Empty;
            foreach (ManagementObject mo in baseObjects)
            {
                id = mo["SerialNumber"].ToString();
                break;
            }
            identification.MainBoard = id;

            // cpu
            ManagementObjectSearcher cpuManage = new ManagementObjectSearcher(CPUQueryString);
            ManagementObjectCollection cmpu = cpuManage.Get();
            string cid = string.Empty;
            foreach (ManagementObject mou in cmpu)
            {
                cid = mou["ProcessorId"].ToString();
                break;
            }
            identification.CPU = cid;
            return identification;
        }
    }

    public class IdentificationClass
    {
        public string CPU { get; set; }

        public string ComputerName { get; set; }

        public string MainBoard { get; set; }

        public string HardDisk { get; set; }
    }

}